using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TgcViewer.Utils;

namespace TgcViewer
{
    public partial class MainForm : Form
    {
        static bool applicationRunning;
        /// <summary>
        /// Obtener o parar el estado del RenderLoop.
        /// </summary>
        public static bool ApplicationRunning
        {
            get { return MainForm.applicationRunning; }
            set { MainForm.applicationRunning = value; }
        } 
        
        public MainForm()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
        }


        private void MainForm_Load(object sender, EventArgs e)
        {
            //Show the App before we init
            this.Show();
            this.panel3d.Focus();

            MainForm.ApplicationRunning = true;
            GuiController guiController = GuiController.Instance;
            guiController.initGraphics(this, panel3d);
            this.resetMenuOptions();


            while (MainForm.ApplicationRunning)
            {
                guiController.render();

                // Process application messages
                Application.DoEvents();
            }


            Application.Exit();
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            MainForm.ApplicationRunning = false;
        }

        /// <summary>
        /// Consola de Log
        /// </summary>
        public RichTextBox LogConsole
        {
            get { return logConsole; }
        }

        /// <summary>
        /// ListView de ejemplos
        /// </summary>
        public ListView ListViewExamples
        {
            get { return listViewExamples; }
        }

        private void listViewExamples_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listViewExamples.SelectedItems.Count > 0)
            {
                GuiController.Instance.executeSelectedExample(listViewExamples.SelectedIndices[0]);
            }
        }

        /// <summary>
        /// Texto de ToolStripStatusPosition
        /// </summary>
        public void setStatusPosition(string text)
        {
            toolStripStatusPosition.Text = text;
        }

        /// <summary>
        /// Texto de ToolStripStatusCurrentExample
        /// </summary>
        /// <param name="text"></param>
        public void setCurrentExampleStatus(string text)
        {
            toolStripStatusCurrentExample.Text = text;
        }

        /// <summary>
        /// Tabla de variables de usuario
        /// </summary>
        /// <returns></returns>
        public DataGridView getDataGridUserVars()
        {
            return dataGridUserVars;
        }

        /// <summary>
        /// Panel de Modifiers
        /// </summary>
        /// <returns></returns>
        internal Panel getModifiersPanel()
        {
            return flowLayoutPanelModifiers;
        }

        private void wireframeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (wireframeToolStripMenuItem.Checked)
            {
                GuiController.Instance.D3dDevice.RenderState.FillMode = Microsoft.DirectX.Direct3D.FillMode.WireFrame;
            }
            else
            {
                GuiController.Instance.D3dDevice.RenderState.FillMode = Microsoft.DirectX.Direct3D.FillMode.Solid;
            }
        }

        private void camaraPrimeraPersonaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GuiController.Instance.FpsCamera.Enable = camaraPrimeraPersonaToolStripMenuItem.Checked;
        }

        private void contadorFPSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GuiController.Instance.FpsCounterEnable = contadorFPSToolStripMenuItem.Checked;
        }

        private void listViewExamples_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewExamples.SelectedItems.Count > 0)
            {
                textBoxExampleDescription.Text = listViewExamples.SelectedItems[0].ToolTipText;
            }
        }

        private void ejesCartesianosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GuiController.Instance.AxisLines.Enable = ejesCartesianosToolStripMenuItem.Checked;
        }

        /// <summary>
        /// Setea los valores default de las opciones del menu
        /// </summary>
        internal void resetMenuOptions()
        {
            camaraPrimeraPersonaToolStripMenuItem.Checked = false;
            wireframeToolStripMenuItem.Checked = false;
            contadorFPSToolStripMenuItem.Checked = true;
            ejesCartesianosToolStripMenuItem.Checked = false;
        }






        
    }
}